package client

import (
	"context"
	"google.golang.org/grpc"
	"month5/common/proto/user"
)

type Handler func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := user.NewUserClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func Login(ctx context.Context, req *user.LoginReq) (*user.LoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.Login(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LoginResp), nil
}

func EatList(ctx context.Context, req *user.EatListReq) (*user.EatListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		eatList, err := client.EatList(ctx, req)
		if err != nil {
			return nil, err
		}
		return eatList, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.EatListResp), nil
}

func PayBack(ctx context.Context, req *user.PayBackReq) (*user.PayBackResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		payBack, err := client.PayBack(ctx, req)
		if err != nil {
			return nil, err
		}
		return payBack, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.PayBackResp), nil
}

func OrderList(ctx context.Context, req *user.OrderListReq) (*user.OrderListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		orderList, err := client.OrderList(ctx, req)
		if err != nil {
			return nil, err
		}
		return orderList, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.OrderListResp), nil
}

func CreatedOrder(ctx context.Context, req *user.CreatedOrderReq) (*user.CreatedOrderResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		orderList, err := client.CreatedOrder(ctx, req)
		if err != nil {
			return nil, err
		}
		return orderList, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.CreatedOrderResp), nil
}

func AddCoupon(ctx context.Context, req *user.AddCouponReq) (*user.AddCouponResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		orderList, err := client.AddCoupon(ctx, req)
		if err != nil {
			return nil, err
		}
		return orderList, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AddCouponResp), nil
}

func LookCoupons(ctx context.Context, req *user.LookCouponsReq) (*user.LookCouponsResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		orderList, err := client.LookCoupons(ctx, req)
		if err != nil {
			return nil, err
		}
		return orderList, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LookCouponsResp), nil
}
